﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using qltscd.Models;
using System.Web.Mvc;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;
using qltscd.Helper;
using System.Web.Routing;

namespace qltscd.Controllers
{
    
    public class ThongKeController : BaseController
    {
        protected override void OnActionExecuting (ActionExecutingContext filterContext) {
            base.OnActionExecuting(filterContext);
            if (SessionHelper.QuyenNhom[qltscd.Contansts.KeyContansts.QuyenThongKe] == '0') {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new {
                    controller = "KhongCoQuyen",
                    action = "Index"
                }));
            }
        }

        private QLTBEntities db = new QLTBEntities();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ThongKeTheoDonVi () {
            List<tkThongKeTheoDonVi> data = db.tkThongKeTheoDonVis.ToList();
            return View(data);
        }

        public void XemThongKeTheoDonVi () {
            using (ReportClass rptH = new ReportClass()) {

                rptH.FileName = Server.MapPath("~") + "\\Reports\\rThongKeThietBiTheoDonVi.rpt";
                rptH.Load();
                //rptH.SetDatabaseLogon("sa", "sa");
                //rptH.SetDataSource(db.tkThongKeTheoDonVis);
                rptH.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "ThongKeThietBiTheoDonVi.pdf");
            }
        }

        public ActionResult ThongKeTheoHopDong () {
            List<tkThongKeTheoHopDong> data = db.tkThongKeTheoHopDongs.ToList();
            return View(data);
        }

        public void XemThongKeTheoHopDong () {
            using (ReportClass rptH = new ReportClass()) {

                rptH.FileName = Server.MapPath("~") + "\\Reports\\ThongKeThietBiTheoHopDong.rpt";
                rptH.Load();
                //rptH.SetDatabaseLogon("sa", "sa");
               // rptH.SetDataSource(db.tkThongKeTheoHopDongs);
                rptH.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "ThongKeThietBiTheoHopDong.pdf");
            }
        }
    }
}
